<?php

namespace Bmb\BasicsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

class ContactController extends Controller
{
    public function indexAction()
    {
        return $this->render('BmbBasicsBundle:Contact:index.html.twig');
    }

    /**
     * Send a message to the administrator
     * @param Message $message
     */
    public function sendMailAction($origin) {
        $view = 'BmbBasicsBundle:Contact:mailContact.html.twig';
		$renderRoute = '';
		
		if ($origin == 'home') {
			$renderRoute = 'bmb_basics_homepage';
		} else {
			$renderRoute = 'bmb_basics_contact';
		}
		
        $request = $this->get('request');
		$name = $request->get('bmb_contact_name');
		$email = $request->get('bmb_contact_email');
		$type = $request->get('bmb_contact_type');
		$content = $request->get('bmb_contact_content');

        // mail composition
        $mail = \Swift_Message::newInstance()
            ->setSubject('Mail de contact')
            ->setFrom($email)
            ->setTo('hello@betmybirth.com')
            ->setBody($this->renderView($view, array('name' => $name, 'type' => $type, 'content' => $content)), 'text/html');

        // send the mail
        try {
            $this->get('mailer')->send($mail);
            $this->get('session')->getFlashBag()->add('notice', 'Votre message a bien été envoyé');
			return new RedirectResponse($this->generateUrl($renderRoute));
        } catch (Swift_TransportException $e) {
            throw new \Swift_TransportException;
        }
		
    }
}
